home *** CD-ROM | disk | FTP | other *** search
- /********************************/
- /* File: ParseCommands.c */
- /* */
- /* Parse commands coming in from*/
- /* Hypercard. */
- /* */
- /* Once the object is parsed, it*/
- /* is added to the draw list for*/
- /* the window. */
- /* */
- /* Eventually need to match */
- /* multiple tokens so that we */
- /* can have parameters such as: */
- /* BOLD&ITALIC&OUTLINE */
- /* ---------------------------- */
- /* © 1989 Donald Koscheka */
- /* All Rights Reserved */
- /********************************/
-
- #include "DrawUtils.h"
-
- #define automatic /* fun with the stack */
-
-
- short strNComp( s1, s2, len )
- char *s1;
- char *s2;
- long len;
- /*************************
- *
- *************************/
- {
- short indx;
- short match = 1;
-
- for( indx = 1; indx <= len; indx++ )
- if( *s1++ != *s2++ ){
- match = 0;
- break;
- }
- return( match );
- }
-
-
- long matchToken( buf, tabl )
- Handle buf;
- short tabl;
- /*******************************
- * given an input buffer and the resource
- * id of the parse table, return
- * the token that represents the input
- * string.
- *
- * A token of 0 is returned if no
- * match is found. This way, you
- * can use the first item in the list
- * as the default item!
- *
- * The symbol table should have the
- * format:
- *
- * <string>, <token>
- *
- * where string mathces to the input
- * string and token is that value for
- * a given match.
- *******************************/
- {
- char *bp; /*** pointer to input strings ***/
- Handle strH; /*** handle to parse strings resource ***/
- long token = 0; /*** return the default if no match ***/
- short indx = 0;
- short theID;
- ResType theType;
- short done = 0;
- long len;
- char theNum[31];
- char *np;
- char theName[256];
- char theString[256];
-
- bp = *buf;
-
- /*** Do the comparison in upper case ***/
- /*** ??? NEED THAT TOUPPER MACRO ***/
- /* while( *bp ){
- *bp = toupper( *bp );
- bp++;
- }
- */
-
- strH = GetResource( STRING_TYPE, tabl );
-
- if( strH ){
-
- GetResInfo( strH, &theID, &theType, &theName );
-
- /*** compare the string to the allowable tokens ***/
- indx = 1 ;
-
- while( !done ){
- theString[0] = '\0';
- GetIndString( &theString, tabl, indx );
-
- if( theString[0] == '\0' ){ /* no strings matched the input */
- done = 1;
- }
- else{ /* attempt to match to current str */
-
- PtoCstr( (char *)&theString );
-
- len = 0;
-
- bp = theString;
- while ( *bp != ',' ){
- bp++;
- len++;
- }
-
- if( strncmp( *buf, (char *)theString, len ) == 0){
- /* have a match so extract the token */
-
- /*** move past any garbage in the string ***/
- while( (*bp < '0' || *bp > '9') && *bp != '-')
- bp++;
-
- /*** now copy what bp points to into a ***/
- /*** a pascal style string ***/
-
- theNum[0] = '\0';
- np = &theNum[1];
-
- while( *bp >= '0' && *bp <= '9' ){
- theNum[0]++;
- *np++ = *bp++;
- }
-
- /*** np is a valid p-string ***/
- StringToNum( theNum, &token );
- done = 1;
- }
- else
- indx++;
- }
- }
- }
- return( token );
- }
-
-
- long parseNum( bp )
- char *bp;
- /***********************
- * parse the data stream
- * and return a numeric
- * value. The stream is null
- * terminated.
- *
- ***********************/
- {
- long num = 0;
-
- char theString[256];
- short done = 0;
- char *ps;
-
- /*** move the input pointer until we're looking at a number ***/
- while( *bp && ( *bp < '0' || *bp > '9' ) && *bp != '-' )
- bp++;
-
- /*** copy the data into a pascal string ***/
- ps = theString;
- ps++;
-
- while( *bp >= '0' && *bp <= '9' )
- *ps++ = *bp++;
-
- /*** moved one past the output so try this ***/
- theString[0] = (char)( ps - theString -1 );
- StringToNum( theString, &num );
-
- return( num );
- }
-
-
- void parsefloat( bp, outp )
- char *bp;
- char *outp;
- /***********************
- * return the next substring
- * in bp that represents a
- * floating number.
- *
- * accumulates a conditioned
- * number string into the
- * output buffer.
- *
- * data gets copied into
- * a pascal string.
- *
- * convert (x) to -x
- * eat commas in the number.
- ***********************/
- {
- char *ps = outp;
-
- *ps++ = '\0';
-
- /*** move the input pointer until we're looking at a number ***/
- while( *bp == SPACE )
- bp++;
-
- if( *bp == '(' ) /*** for converting negatives ***/
- *ps++ = '-';
-
- while( *bp && ( *bp < '0' || *bp > '9' ) && *bp != '-' && *bp != '.' && *bp != '+' )
- bp++;
-
- /*** copy the data into a pascal string ***/
- while( ( *bp >= '0' && *bp <= '9' ) || *bp == '.' || *bp == '-' || *bp == ',')
- if( *bp == ',' )
- *bp++;
- else
- *ps++ = *bp++;
-
- /*** moved one past the output so try this ***/
- *outp = (char)(ps - outp - 1);
- }
-
-
-
- char *nextToken( bp )
- char *bp;
- /***********************
- * given a pointer to an
- * input stream, move to the
- * next token in the stream.
- *
- * Token's are delineated by
- * ',' or whitespace
- *
- * Assumes we are pointing to the current token
- * Move past the current token and the white
- * space that follows it.
- ***********************/
- {
- /*** move past the current token ***/
- while( *bp &&(*bp != ',' && *bp != SPACE && *bp != CR && *bp != LF && *bp != TAB) )
- bp++;
-
- /*** move past the white space to the next token ***/
- while( *bp == ',' || *bp == SPACE || *bp == CR || *bp == LF || *bp == TAB )
- bp++;
-
- return( bp );
- }
-
-
- void parseRect( buf, theRect )
- Handle buf;
- Rect *theRect;
- /***********************
- * parse the data stream
- * into a rectangle
- ***********************/
- {
- char *bp;
-
- HLock( buf );
-
- bp = *buf;
- theRect->top = parseNum( bp );
- bp = nextToken( bp );
-
- theRect->left = parseNum( bp );
- bp = nextToken( bp );
-
- theRect->bottom = parseNum( bp );
- bp = nextToken( bp );
-
- theRect->right = parseNum( bp );
- bp = nextToken( bp );
-
- HUnlock( buf );
- }
-
-
-
- dataHand Parse_Title( paramPtr )
- XCmdBlockPtr paramPtr;
- /*******************************
- * Draw a title on the screen:
- * command looks like this:
- *
- * params[0] == "DRAW"
- * params[1] == <window name>
- * params[2] == "TITLE"
- * params[3] == the string
- * params[4] == the bounding rect
- * params[5] == the font
- * params[6] == the size
- * params[7] == the style
- * params[8] == the justification
- * params[9] == color
- *
- * Defaults:
- * whole screen, Geneva, 9, plain, left
- *
- * Object Format:
- * <rect><font><size><style><just><text>
- *******************************/
- {
- Rect theRect;
- titlHand theTitle = NIL;
- titlPtr tp;
- short theFont;
- long siz;
- Handle theText;
- short temp;
- char *fp1;
- char *fp2;
- char fontName[256];
-
- theTitle = (titlHand)NewHandle( sizeof( titleObj ) );
-
- if( theTitle ){
- /*** set the defaults ***/
- tp = *theTitle;
- tp->theRect.top = 0;
- tp->theRect.left= 0;
- tp->theRect.bottom = 100;
- tp->theRect.right = 100;
-
- tp->font = 3;
- tp->size = 9;
- tp->style = 0;
- tp->just = teJustLeft;
- tp->color = blackColor;
- tp->text = NIL;
-
- /*** parse the parameter list ***/
- theText = paramPtr->params[3];
- HandToHand( &theText );
-
- HLock( theTitle );
- tp = *theTitle;
- tp->text = theText;
-
- if( paramPtr->params[4] ){
- parseRect( paramPtr->params[4], &theRect );
- tp->theRect.top = theRect.top;
- tp->theRect.left = theRect.left;
- tp->theRect.bottom = theRect.bottom;
- tp->theRect.right = theRect.right;
- }
-
- if( paramPtr->params[5] ){
- siz = GetHandleSize( paramPtr->params[5] )-1;
- fontName[0] = '\0';
- fp1 = fontName + 1;
- fp2 = *(paramPtr->params[5]);
-
- while( *fp2 )
- *fp1++ = *fp2++;
-
- fontName[0] = (char)siz;
- GetFNum( fontName, &theFont );
-
- tp = *theTitle;
- tp->font = theFont;
- }
-
- if( paramPtr->params[6] )
- tp->size = parseNum( *(paramPtr->params[6] ) );
-
- if( paramPtr->params[7] ){
- temp = (short)matchToken( paramPtr->params[7], TEXT_STYLES );
- tp->style = temp;
- }
-
- if( paramPtr->params[8] ){
- temp = (short)matchToken( paramPtr->params[8], TEXT_JUSTIFICATION );
- tp->just = temp;
- }
-
- if( paramPtr->params[9] ){
- temp = (short)matchToken( paramPtr->params[9], COLORS );
- tp->color = temp;
- }
-
- HUnlock( theTitle );
-
- (*theTitle)->token= TITLE;
- }/* if theTitle */
-
- return( (dataHand)theTitle );
- }
-
-
-
- /*** consider is_white and is_number as macros! ***/
-
- is_white( tc )
- char tc;
- /****************************
- * determine whether the character is
- * white space and return the result
- *
- * Currently only accepting space
- * and tab as white space.
- ****************************/
- {
- return( tc == SPACE || tc == TAB );
- }
-
- is_digit( tc )
- char tc;
- /****************************
- * return true if the next character
- * in the input is a digit:
- *
- * should be a macro
- *
- * digit ::= 0|1|2|3|4|5|6|7|8|9
- ****************************/
- {
- if( (tc >= '0' && tc <= '9') )
- return( TRUE );
-
- return( FALSE );
- }
-
-
- is_number( tp )
- char *tp;
- /****************************
- * return true if the next character
- * in the input is a number:
- *
- * digit ::= 0|1|2|3|4|5|6|7|8|9
- * sign ::= -, (
- * number::= sign|digit
- *
- ****************************/
- {
- register tc = *tp++;
-
- if( (tc >= '0' && tc <= '9') || tc == '+' || tc == '(' || tc == '$' )
- return( TRUE );
-
- if( (tc == '-' || tc == '.') && is_digit( *tp ) )
- return( TRUE );
-
- return( FALSE );
- }
-
-
- char *skip_spaces( tp )
- char *tp;
- /****************************
- * move thru the input string
- * until the next character is non-
- * space.
- ****************************/
- {
- while( *tp && (*tp == SPACE || *tp == TAB) )
- tp++;
-
- return( tp );
- }
-
- char *skip_to_number( tp )
- char *tp;
- /****************************
- * move thru the input string
- * until the next character is non-
- * space.
- ****************************/
- {
- while( *tp && (*tp == SPACE || *tp == TAB || *tp == '%' || *tp == '$' ) )
- tp++;
-
- return( tp );
- }
-
- char *skip_white( tp )
- char *tp;
- /****************************
- * move thru the input string
- * until the next character is non-
- * white.
- ****************************/
- {
- while( *tp && ( *tp == SPACE || *tp == TAB || *tp == CR) )
- tp++;
-
- return( tp );
- }
-
-
- char *skip_literal( tp )
- char *tp;
- /****************************
- * move thru the input string
- * until the next character is a
- * space, tab, return or null.
- *
- * return the value of the pointer
- *
- ****************************/
- {
- while( *tp && *tp != SPACE && *tp != TAB && *tp != CR )
- tp++;
-
- return( tp );
- }
-
-
-
- short matches_literal( inp )
- char *inp;
- /****************************
- * append the given character to the
- * end of the handle.
- *
- * currently handles:
- * NA, N/A, na, n/a
- ****************************/
- {
-
- if( *inp == 'N' || *inp == 'n' ){
- inp++;
- if( *inp == 'A' || *inp == 'a' || *inp == '/' ){
-
- *inp++ = '0';
-
- while( !is_white( *inp ) && *inp != CR && *inp != '\0' )
- *inp++ = ' ';
-
- return( TRUE );
- }
- }
-
- return( FALSE );
- }
-
-